package chapter10_Advanced._communication;

/**
 * ClassName: PrintNumberTest
 * Package: chapter10_Advanced._communication
 * Description:
 *
 * @Author SherryAn
 * @Create 2025/8/12 19:06
 */

//Runnable是函数式接口，作用为创建线程
class PrintNumber implements Runnable{

    private int number=1;
    @Override
    public void run() {
        while(true){
            synchronized (this) {
                notify();
                if (number <= 100) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;
                    try {
                        //线程一旦执行此方法，会进入等待状态。同时会释放同步监视器的调用
                        wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    break;
                }
            }
        }
    }

}

public class PrintNumberTest {
    public static void main(String[] args) {

        PrintNumber p = new PrintNumber();
        //创建线程
        Thread t1 =new Thread(p,"线程一");
        Thread t2 =new Thread(p,"线程二");



        t1.start();
        t2.start();
    }
}
